﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2013-03-21 20:07:26Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Threading.Tasks;

using Windows.Storage;

namespace Outcoder.Services.Implementation
{
	public class RoamingSettingsStore : ISettingsStore
	{
		ApplicationDataContainer settings_UseGetSettings;

		ApplicationDataContainer GetSettings()
		{
			if (settings_UseGetSettings == null)
			{
				settings_UseGetSettings = ApplicationData.Current.RoamingSettings;
			}

			return settings_UseGetSettings;
		}

		public bool TryGetValue(string key, Type settingType, out object value)
		{
			var settings = GetSettings();
			return settings.Values.TryGetValue(key, out value);
		}

		public bool Contains(string key)
		{
			var settings = GetSettings();
			return settings.Values.ContainsKey(key);
		}

		public bool Remove(string key)
		{
			var settings = GetSettings();
			return settings.Values.Remove(key);
		}

		public Task Clear()
		{
			var settings = GetSettings();
			settings.Values.Clear();

			return Task.FromResult(0);
		}

		public Task Save()
		{
			/* Nothing to do. */
			return Task.FromResult(0);
		}

		public object this[string key]
		{
			get
			{
				var settings = GetSettings();
				return settings.Values[key];
			}
			set
			{
				var settings = GetSettings();
				settings.Values[key] = value;
			}
		}

		public SettingsStoreStatus Status
		{
			get
			{
				return SettingsStoreStatus.Ready;
			}
		}
	}
}
